from optparse import OptionParser
import time

import os, sys
try:
    APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
except KeyError:
    os.environ['SIMPLEWATCHER_INSTALL'] = APPLICATION_INSTALL_DIR = "/opt/docmanagement"
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from docmanagement.watcher.watch import Watch

def main():
    confDir = "%s/conf/autocommission" % APPLICATION_INSTALL_DIR
    watch = Watch("watch_autocommission", confDir)
    while True:
        files = watch.queue.read()
        for file in files:
            file_with_path = os.path.join(watch.queue.config.path.xml_text_content(), file)
            watch.run(file_with_path)
        time.sleep(int(watch.config.rest.xml_text_content()))



if __name__ == "__main__":
    ld = os.environ.get('LD_LIBRARY_PATH')
    if ld is None:
        os.environ['LD_LIBRARY_PATH'] = "/opt/group1/doc1gen"
    else:
        os.environ['LD_LIBRARY_PATH'] = ld + ":/opt/group1/doc1gen"
    main()
